# _*_ coding:utf-8 _*_
__author__ = 'joker_wb'
__date__ = '2018/3/18 20:14'
from users.models import EmailVerifyRecord
from random import Random

from django.core.mail import send_mail#发送邮件函数。

from MxOnline.settings import EMAIL_FROM
def generate_random_str(randomlength=8):
    """
     随机生成一个字符串
    :param randomlength: 生成字符串的长度，默认为8
    :return: 返回该字符串
    """


    str=''
    chars='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length=len(chars)
    random=Random()
    for i in range(randomlength):
        str+=chars[random.randint(0,length-1)]
    return str

def send_register_email(email,send_type="register"):
    email_record=EmailVerifyRecord()
    code=generate_random_str(16)
    email_record.code=code
    email_record.email=email
    email_record.send_type=send_type
    email_record.save()

    email_title=""#收到邮件的标题
    email_body=""
    if(send_type=='register'):#如果是注册邮件
        email_title='王博学习网注册激活链接'
        email_body='请点击下边的链接激活你的账号：http://127.0.0.1:8000/active/{0}'.format(code)
        send_status=send_mail(email_title,email_body,EMAIL_FROM,[email])#最后一个参数必须的list
        if send_status:#发送成功
            pass
    else:
        email_title = '王博学习网注册密码重置'
        email_body = '请点击下边的链接激活你的账号：http://127.0.0.1:8000/reset/{0}'.format(code)
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])  # 最后一个参数必须的list



